const Txt=require("../models/txt")

//获取所有博客信息
exports.getboke=async function (){
    const result=await Txt.findAll({
        order:[
            ['createdAt','ASC']
        ]
    })
    return result;
}
// 分页查询
const {
    Op, where
} = require("sequelize")
exports.getfenye= async function (page = 1, limit = 10, keywords = "") {
    const where = {
        [Op.or]: [{
                name: {
                    [Op.like]: `%${keywords}%`
                }
            },
        ]
    };
    const result = await Txt.findAndCountAll({
        order:[
            ['createdAt','DESC']
        ],
        offset: (page - 1) * limit,
        limit: +limit,
        where
    })
    return JSON.parse(JSON.stringify(result))
}
//查
exports.getIdfind = async function (page = 1, limit = 10, id) {
    const result = await Txt.findAndCountAll({
        offset: (page - 1) * limit,
        limit: +limit,
        where: {
            id
        }
    })
    return JSON.parse(JSON.stringify(result))
}
//模糊查询
exports.getMhfind = async function (page = 1, limit = 10, keywords = ""){
    const where = {
        [Op.or]: [{
                name: {
                    [Op.like]: `%${keywords}%`
                }
            },
        ]
    };
    const result = await Txt.findAndCountAll({
        offset: (page - 1) * limit,
        limit: +limit,
        where
    })
    return JSON.parse(JSON.stringify(result))
}
//添加修改
exports.updateBoke = async function (id, obj) {
    return await Txt.update(obj, {
        where: {
            id
        }
    });
}
//热度查询
exports.getHot = async function (page = 1, limit = 10,){
    const result=await Txt.findAll({
        order:[
            ['hot','DESC']
        ],
        // include:[userpl],
        offset:(page-1)*limit,
        limit:+limit,
    })
    return result;
}
//增加阅读量
exports.updateHot = async function (id, hot){
    return await Txt.update(hot,{
        where:{
            id
        }
    })
}
//分类
exports.getFl = async function (page = 1, limit = 10, keywords = ""){
    const where = {
        [Op.or]: [{
                lx: {
                    [Op.like]: `%${keywords}%`
                }
            },
        ]
    };
    const result = await Txt.findAndCountAll({
        order:[
            ['createdAt','DESC']
        ],
        offset: (page - 1) * limit,
        limit: +limit,
        where
    })
    return JSON.parse(JSON.stringify(result))
}
//添加博客
exports.addBk = async function (bkObj){
    const ins = await Txt.create(bkObj)
    return ins.toJSON()
}